package com.fs.cv.controller;

import com.fs.cv.pojo.pojodao.Class;
import com.fs.cv.pojo.pojodao.Headmaster;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.pojo.pojoview.ClassHeadmaster;
import com.fs.cv.pojo.pojoview.ClassInfo;
import com.fs.cv.pojo.pojoview.StudentInfo;
import com.fs.cv.service.ClassManagementService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>班级管理</p>
 *
 * @author : 苏明友
 * @date : 2020-10-09 23:16
 **/
@Controller
@RequestMapping("/classManagement")
public class ClassManagementController {
    @Autowired
    private ClassManagementService classManagementService;

    /**
     * 方法描述:根据user_id查询到具体的教务主任，然后教务主任再通过自己的专业id查询到自己所管理的班级,显示在前端页面
     *
     * @参数 []
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/10
     */
    @RequestMapping("/getClassInfoById")
    @ResponseBody
    public Map<String, Object> getAllClassInfoToView() {
        Map<String, Object> result = new HashMap<>();
        //获取存储的session
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        System.out.println("userId = " + userId);
        List<ClassInfo> classInfoList = classManagementService.getAllClassInfoToView(userId);
        result.put("code", 0);
        result.put("message", "获取班级信息成功");
        result.put("classInfoList", classInfoList);
        return result;
    }

    /**
     * 方法描述:根据userId来查询教务主任的专业ID
     *
     * @参数 []
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/10
     */
    @RequestMapping("/getDepartmentIdByUserId")
    @ResponseBody
    public Map<String, Object> getDepartmentIdByUserId() {
        //获取存储的session
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        System.out.println("userId = " + userId);
        List<ClassInfo> departmentInfoList = classManagementService.getDepartmentIdByUserId(userId);
        System.out.println("departmentInfoList = " + departmentInfoList);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取班级信息成功");
        result.put("departmentInfoList", departmentInfoList);
        return result;
    }

    /**
     * 方法描述:向数据库中添加一个班级
     *
     * @参数 [classInfo]
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/10
     */
    @RequestMapping("/addClassById")
    @ResponseBody
    public Map<String, Object> addClassInfo(ClassHeadmaster classHeadmaster) {
        System.out.println("classInfo = " + classHeadmaster);
        int x = classManagementService.addClassInfo(classHeadmaster);
        //int z = classManagementService.addHeadmasterInfo(classHeadmaster);
        //int y = x+z;
        System.out.println("x = " + x);
        Map<String, Object> result = new HashMap<>();
        if (x > 0) {
            result.put("message", "获取数据成功");
            result.put("code", 0);
        }
        return result;
    }

    /**
     * 方法描述:根据classId逻辑删除班级信息
     *
     * @参数 [classId]
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/11
     */
    @RequestMapping("/deleteClassInfoByClassId")
    @ResponseBody
    @Transactional
    public Map<String, Object> deleteClassInfoByClassId(Integer classId) {
        System.out.println("classId = " + classId);
        Map<String, Object> result = new HashMap<>();
        int deleted = classManagementService.deleteClassInfoByClassId(classId);
        int z = classManagementService.updateHeadmasterStateByClassId(classId);
        int a = deleted + z;
        System.out.println("deleted = " + deleted);
        if (a >= 2) {
            result.put("code", 0);
            result.put("message", "删除用户数据成功");
        } else {
            result.put("code", -1);
            result.put("message", "删除用户数据失败");
        }
        return result;
    }

    /**
     * 方法描述:根据classId查询那些班主任没有管理对应的班级-->未完成，不影响运行
     *
     * @参数 []
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/11
     */
    @RequestMapping("/getOneHeadmasterInfoByClassId")
    @ResponseBody
    public Map<String, Object> getOneHeadmasterInfoByClassId(Integer classId) {
        System.out.println("classId = " + classId);
        List<Headmaster> headmasterInfo = classManagementService.getOneHeadmasterInfoByClassId();
        System.out.println("headmasterInfo = " + headmasterInfo);
        List<Class> classList = classManagementService.getOneClassInfo(classId);
        System.out.println("classList = " + classList);
        Map<String, Object> result = new HashMap<>();
        result.put("headmasterInfo", headmasterInfo);
        result.put("classList", classList);
        result.put("code", 0);
        result.put("message", "删除用户数据成功");
        return result;
    }

    @RequestMapping("/updateHeadmasterInfo")
    @ResponseBody
    public Map<String, Object> updateHeadmasterInfo(Headmaster headmaster) {
        System.out.println("headmaster = " + headmaster);
        Map<String, Object> result = new HashMap<>();
        int u = classManagementService.updateHeadmasterInfo(headmaster);
        System.out.println("u = " + u);
        if (u > 0) {
            result.put("code", 0);
            result.put("message", "分配班主任成功");
        }
        return result;
    }

}